"""
des: 替换等操作 输入区域 根据功能变更输入区域布局
author: mr_52hz
date: 2021-01-29
"""
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLineEdit

import settings


class InputWidget(QWidget):

    is_input_ready_signal = pyqtSignal(bool)

    def __init__(self, flag, parent=None, *args, **kwargs):
        super(InputWidget, self).__init__(parent, *args, **kwargs)
        self.func = settings.RENAME_FUNCS.get(flag, 'replace')
        self.input_layout = QVBoxLayout()
        self.input_layout.setContentsMargins(0, 0, 0, 0)
        self.first_line_edit = None
        self.second_line_edit = None
        self.initUI()

    def initUI(self):
        self.setMinimumWidth(settings.PATH_LABEL_WIDTH)
        self.setFixedHeight(settings.PATH_LABEL_HEIGHT * 3)

        self._addFuncWidgetsToLayout(self._getFuncWidgets())

        self.setLayout(self.input_layout)

    def changeFuncWidgets(self, flag):
        """
        重构输入界面
        :param flag:
        :return:
        """
        func = settings.RENAME_FUNCS.get(flag, 'replace')
        if self.func == func:
            return

        self.func = func
        self._deleteFuncWidgetsFromLayout()
        self._addFuncWidgetsToLayout(self._getFuncWidgets())

    def isInputReady(self):
        first_param = self.first_line_edit.text()
        second_param = self.second_line_edit.text()
        if first_param:
            if self.func in ['replace', 'changeFormat', 'reZeroFill']:
                if second_param and first_param != second_param:
                    self.is_input_ready_signal.emit(True)
                    return
                self.is_input_ready_signal.emit(False)
                return
            self.is_input_ready_signal.emit(True)
            return
        self.is_input_ready_signal.emit(False)
        return

    def _addFuncWidgetsToLayout(self, widgets):
        for widget in widgets:
            self.input_layout.addWidget(widget)

    def _deleteFuncWidgetsFromLayout(self):
        for i in range(self.input_layout.count()):
            self.input_layout.itemAt(i).widget().deleteLater()

    def _getFuncWidgets(self):
        widgets = []
        self.first_line_edit = QLineEdit(self)
        self.second_line_edit = QLineEdit(self)
        self.first_line_edit.setMinimumSize(settings.PATH_LABEL_WIDTH, settings.PATH_LABEL_HEIGHT)
        self.second_line_edit.setMinimumSize(settings.PATH_LABEL_WIDTH, settings.PATH_LABEL_HEIGHT)
        # 左 上 右 下
        self.first_line_edit.setTextMargins(2, 4, 2, 4)
        self.second_line_edit.setTextMargins(2, 4, 2, 4)

        self.first_line_edit.textEdited.connect(self.isInputReady)
        self.second_line_edit.textEdited.connect(self.isInputReady)

        if self.func == 'replace':
            self.first_line_edit.setPlaceholderText('origin str you want to replace')
            self.second_line_edit.setPlaceholderText('target str')
            widgets.append(self.first_line_edit)
            widgets.append(self.second_line_edit)
            return widgets
        elif self.func == 'delete':
            self.first_line_edit.setPlaceholderText('delete str')
            widgets.append(self.first_line_edit)
            return widgets
        elif self.func == 'addPrefix':
            self.first_line_edit.setPlaceholderText('prefix str like:  [python]- ')
            widgets.append(self.first_line_edit)
            return widgets
        elif self.func == 'addSuffix':
            self.first_line_edit.setPlaceholderText('suffix str like:  _python')
            widgets.append(self.first_line_edit)
            return widgets
        elif self.func == 'changeFormat':
            self.first_line_edit.setPlaceholderText('format want to replace')
            self.second_line_edit.setPlaceholderText('target format')
            widgets.append(self.first_line_edit)
            widgets.append(self.second_line_edit)
            return widgets
        elif self.func == 're':
            self.first_line_edit.setPlaceholderText('re pattern')
            self.second_line_edit.setPlaceholderText('target str or pattern')
            widgets.append(self.first_line_edit)
            widgets.append(self.second_line_edit)
            return widgets
        elif self.func == 'reZeroFill':
            self.first_line_edit.setPlaceholderText('[0-9-]+(?P<suffix>_[0-9]+) 注: suffix保留数字特征_')
            self.second_line_edit.setPlaceholderText('zero fill num length')
            widgets.append(self.first_line_edit)
            widgets.append(self.second_line_edit)
            return widgets
        else:
            raise KeyError('rename func do not support!')
